<?php

switch (@$URL[1]) {
    case '':
    default:
        break;
    
    case 'list':
        
        function big_id( $a, $b )
        {
            if ( $a['Torrent_id'] == $b['Torrent_id'] )
            {
                return 0;
            }
            return ( $a['Torrent_id'] > $b['Torrent_id'] ) ? -1 : 1;
        }
        
        $downloaded = $Torrent->get_all();
        
        usort( $downloaded, 'big_id' );
        
        $current_page->assign( 'torrents', $downloaded );
        $_page_content .= $current_page->fetch( 'torrents_list.tpl' );
        
        break;
    
    case 'details':
        
        $torrent_id = (int)$URL[2];
        
        $torrent_data = $Speed->find_by( 'torrent', $torrent_id );
        
        $torrent_details = $Torrent->find_by( 'id', $torrent_id );
        $torrent_details = $torrent_details[0];
        
        foreach( $torrent_data as &$t )
        {
            $aux = explode( " ", $t[$Speed->TABLE_COLUMNS['timestamp']] );
            $aux_date = explode( "-", $aux[0] );
            $aux_time = explode( ":", $aux[1] );
            
            $t['date']['year'] = $aux_date[0];
            $t['date']['month'] = $aux_date[1];
            $t['date']['day'] = $aux_date[2];
            $t['date']['hour'] = $aux_time[0];
            $t['date']['minute'] = $aux_time[1];
            $t['date']['second'] = $aux_time[2];
            $t['date']['millisecond'] = 0;
            
            $t[$Speed->TABLE_COLUMNS['speed']] *= 10;
        }
        
        $watchlist = $Watchlist->find_by( 'torrent', $torrent_id );
        foreach( $watchlist as &$t )
        {
            $aux = explode( " ", $t[$Watchlist->TABLE_COLUMNS['timestamp']] );
            $aux_date = explode( "-", $aux[0] );
            $aux_time = explode( ":", $aux[1] );
            
            $t['date']['year'] = $aux_date[0];
            $t['date']['month'] = $aux_date[1];
            $t['date']['day'] = $aux_date[2];
            $t['date']['hour'] = $aux_time[0];
            $t['date']['minute'] = $aux_time[1];
            $t['date']['second'] = $aux_time[2];
            $t['date']['millisecond'] = 0;            
        }
        
        $current_page->assign( 'torrent', $torrent_data );
        $current_page->assign( 'torrent_details', $torrent_details );
        $current_page->assign( 'torrent_watchlist', $watchlist );
        
        //my_DBG( $torrent_data );
        
        $_page_content .= $current_page->fetch( 'torrents_details.tpl' );
        
        break;
    
}
?>
